<?php
	//include "./FanJianConvert.php";	
	/*
		语种代码：
			中文：zh
			英文：en
			繁体：cht

							被翻译的文件名  翻译后的文件名 被翻译的语种  翻译后的语种
		php Translation.php		main.js     dest_main.js     zh				en
	*/	

	$oFileName = $argv[1];
	$dFileName = $argv[2];
	$oLangType = $argv[3];
	$dLangType = $argv[4];


	/**
     * @param        $str
     * @param        $to
     * @param string $from
     *
     * @return bool
     */
	function transWithBaidu($str, $to, $from = 'zh')
    {
		$data = array('from' => $from, 'to' => $to, 'query' => $str, 'transtype' => 'trans', 'simple_means_flag' => 3);
        $url  = "http://fanyi.baidu.com/v2transapi";
        $ch   = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $output = json_decode(curl_exec($ch), TRUE);

		curl_close($ch);
        
		if (is_array($output) && count($output) > 1 && array_key_exists('trans_result', $output) && array_key_exists('data', $output['trans_result'])) {
            if (isset($output['trans_result']['data'][0]['dst'])) return $output['trans_result']['data'][0]['dst'];
        }

        return FALSE;
    }	
	
	if(!file_exists($oFileName)){
		echo $oFileName."不存在，请检查！\n";
		return false;
	}

	$json = file_get_contents($oFileName);

	$format_json = str_replace("'","\"",$json);

	$json_arr = json_decode($format_json,true);

	if(empty($json_arr) || !is_array($json_arr))
	{
		echo "文件内容非标准的json格式，请检查！\n";
	}

	$content = "{\n";
	
	foreach($json_arr as $k=>$v){
		
		$content .= "\t\"".$k.'":"'.transWithBaidu($v,$dLangType,$oLangType).'",'."\n"; 
	}
	
	$content .='}';

	file_put_contents($dFileName,$content);
   
	echo "create ${dFileName} success!\n";


